﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Linq;

namespace DienThoai
{
	/// <summary>
	/// Interaction logic for StaffControl.xaml
	/// </summary>
	public partial class StaffControl : Window
	{
		public StaffControl()
		{
			this.InitializeComponent();
			
			// Insert code required on object creation below this point.
		}
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DienThoaiDataContext db = new DienThoaiDataContext();
            var Roles = from i in db.Roles
                        where i.RoleID != "1"
                        select i.RoleName;
            foreach (var role in Roles)
                cbbRole.Items.Add(role);
            cbbRole.SelectedIndex = 0;
        }

        private string getID()
        {
            DienThoaiDataContext db = new DienThoaiDataContext();
            string ID = "";

            var query = from i in db.Staffs
                        select i.PersonID;

            if (query.Count() == 0)
                ID = "NV00001";
            else
            {
                int temp = 0;
                foreach (var i in query)
                    temp = int.Parse(i.ToString().Substring(2)) + 1;
                if (temp < 10)
                    ID = "NV0000" + temp.ToString();
                else if (temp < 100)
                    ID = "NV000" + temp.ToString();
                else if (temp < 1000)
                    ID = "NV00" + temp.ToString();
                else if (temp < 10000)
                    ID = "NV0" + temp.ToString();
                else
                    ID = "NV" + temp.ToString();
            }
            return ID;
        }

        private void btCreateNew_Click(object sender, RoutedEventArgs e)
        {
            DienThoaiDataContext db = new DienThoaiDataContext();            
            float number;
            bool isExisted = false;
            var StaffList = from i in db.Staffs
                            select i.Username;
            

            if (txtUsername.Text == "")
                MessageBox.Show("Chưa nhập UserName");
            else if (txtPassword.Text == "")
                MessageBox.Show("Chưa nhập Password");
            else if (txtFullName.Text == "")
                MessageBox.Show("Chưa nhập họ tên đầy đủ");
            else if (dpDOB.SelectedDate == null)
                MessageBox.Show("Chưa chọn ngày sinh");
            else if (txtAddress.Text == "")
                MessageBox.Show("Chưa nhập địa chỉ");
            else if (txtPhone.Text != "" && float.TryParse(txtPhone.Text, out number) == false)
                MessageBox.Show("Số điện thoại có chứa ký tự");
            else
            {
                foreach (var existStaff in StaffList)
                    if (txtUsername.Text == existStaff)
                        isExisted = true;
                if (isExisted)
                    MessageBox.Show("Username đã tồn tại trong CSDL. Xin chọn UserName khác.");
                else
                {
                    Staff newStaff = new Staff();
                    Person newPerson = new Person();
                    newPerson.PersonID = getID();
                    newPerson.Fullname = txtFullName.Text;
                    newPerson.DOB = dpDOB.SelectedDate;
                    newPerson.Address = txtAddress.Text;
                    newPerson.Phone = txtPhone.Text;
                    db.Persons.InsertOnSubmit(newPerson);

                    newStaff.PersonID = newPerson.PersonID;
                    newStaff.Username = txtUsername.Text;
                    newStaff.Password = txtPassword.Text;
                    newStaff.Date = DateTime.Today;
                    var role = from i in db.Roles
                               where i.RoleName == cbbRole.SelectedValue.ToString()
                               select i.RoleID;
                    foreach (var i in role)
                        newStaff.RoleID = i;
                    db.Staffs.InsertOnSubmit(newStaff);
                    db.SubmitChanges();
                    DialogResult = true;
                }
            }  
            
            
        }

        private void btCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
	}
}